package mercurius.fitness.client.rb.message;

import java.util.ArrayList;
import java.util.Date;
import net.pleso.framework.client.bl.rb.columns.impl.RBColumn;
import net.pleso.framework.client.dal.IDataColumn;
import net.pleso.framework.client.dal.IDataRow;
import net.pleso.framework.client.dal.db.IDBValue;
import net.pleso.framework.client.dal.db.types.DBDate;
import net.pleso.framework.client.dal.db.types.DBInteger;
import net.pleso.framework.client.dal.db.types.DBString;
import net.pleso.framework.client.dal.db.types.DBTime;
import net.pleso.framework.client.dal.impl.DataColumn;

public class Message extends MessageInfo implements IDataRow {

	public Message() {

	}

	public Message(String level,String message) {
		super(new Integer(MessageDataSet.size() +1),  new Date(),level,message);
	}

	public class id_column extends Columns {
		public id_column() {
			super("id", "Message ID", false);
		}

		public IDBValue getCell(Message row) {
			return new DBInteger(row.getID());
		}

		public void setCell(Message row, IDBValue value) {
			if (value instanceof DBInteger)
				row.setID(((DBInteger) value).getInteger());
		}
	}

	public class message_column extends Columns {

		public message_column() {
			super("message", "Message", false);
		}

		public IDBValue getCell(Message row) {
			return new DBString(row.getMessage());
		}

		public void setCell(Message row, IDBValue value) {
			if (value instanceof DBString)
				row.setMessage(((DBString) value).getString());
		}
	}
	
	public class level_column extends Columns {

		public level_column() {
			super("level", "Level", false);
		}

		public IDBValue getCell(Message row) {
			return new DBString(row.getLevel());
		}

		public void setCell(Message row, IDBValue value) {
			if (value instanceof DBString)
				row.setLevel(((DBString) value).getString());
		}
	}

	
	public class date_column extends Columns {

		public date_column() {
			super("date", "Date", false);
		}

		public IDBValue getCell(Message row) {
			return new DBDate(row.getDate());
		}

		public void setCell(Message row, IDBValue value) {
			if (value instanceof DBDate)
				row.setDate(((DBDate) value).getDate());
		}
	}
	
	public class time_column extends Columns {

		public time_column() {
			super("time", "Time", false);
		}

		public IDBValue getCell(Message row) {
			return new DBTime(row.getDate());
		}

		public void setCell(Message row, IDBValue value) {
			if (value instanceof DBTime)
				row.setDate(((DBTime) value).getTime());
		}
	}


	private static ArrayList columns = new ArrayList();

	private static final Message instance = new Message();

	public static RBColumn[] getColumns() {
		RBColumn[] rbColumns = new RBColumn[] {
			//	new RBColumn(Message.Columns.id, 30),
				new RBColumn(Message.Columns.date, 4),
				new RBColumn(Message.Columns.time, 4),
				new RBColumn(Message.Columns.level, 5),
				new RBColumn(Message.Columns.message, 30)
				};
		return rbColumns;
	}

	public abstract static class Columns extends DataColumn {

		public Columns(String name, String caption, boolean allowNull) {
			super(name, caption, allowNull);
			columns.add(this);
			this.order = columns.size();
		}

		public abstract void setCell(Message row, IDBValue value);

		public abstract IDBValue getCell(Message row);

		public static final id_column id = instance.new id_column();

		public static final message_column message = instance.new message_column();

		public static final date_column date = instance.new date_column();
		public static final time_column time = instance.new time_column();
		public static final level_column level = instance.new level_column();

		}

	public IDBValue getCell(IDataColumn column) {
		Columns col = (Columns) column;
		return col.getCell(this);
	}

	public IDataColumn getColumn(int index) {
		return (IDataColumn) columns.get(index);
	}

	public int getColumnCount() {
		return columns.size();
	}

	public void setCell(IDataColumn column, IDBValue value) {
		Columns col = (Columns) column;
		col.setCell(this, value);
	}
	

}
